// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: R Spike Chain Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 59 : TYPE_RSPIKECHAIN

// Function declarations
reserve function RSpikeChain_DebugDraw
reserve function RSpikeChain_DebugSpawn

// Static Values
public value RSpikeChain_value34 = 0;

// Tables

function RSpikeChain_DebugDraw
	DrawSprite(0)
	temp0 = object[0].value17
	temp0 -= RSpikeChain_value34
	switch temp0
	case 0
	case 1
		temp0 = 4
		break
	case 2
	case 3
		temp0 = 2
		break
	end switch
	temp1 = object.ypos
	while temp0 > 0
		temp1 -= 0x100000
		DrawSpriteXY(1, object.xpos, temp1)
		temp0--
	loop
end function


function RSpikeChain_DebugSpawn
	CreateTempObject(TypeName[R Spike Chain], object.direction, object.xpos, object.ypos)
	temp0 = object[0].value17
	temp0 -= RSpikeChain_value34
	switch temp0
	case 0
		object[tempObjectPos].value0 = 5
		object[tempObjectPos].value1 = 4
		break
	case 1
		object[tempObjectPos].value0 = 2
		object[tempObjectPos].value1 = 4
		break
	case 2
		object[tempObjectPos].value0 = 2
		object[tempObjectPos].value1 = 2
		break
	case 3
		object[tempObjectPos].value0 = -2
		object[tempObjectPos].value1 = 2
		break
	end switch
end function


event ObjectMain
	object.angle += object.value0
	if object.angle < 0
		object.angle += 512
	end if
	object.angle &= 511
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = 0
		temp1 = 16
		temp2 = object.xpos
		temp3 = object.ypos
		while temp0 < object.value1
			Cos(object.xpos, object.angle)
			object.xpos *= temp1
			object.xpos <<= 7
			object.xpos += temp2
			Sin(object.ypos, object.angle)
			object.ypos *= temp1
			object.ypos <<= 7
			object.ypos += temp3
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_Hit)
			end if
			temp0++
			temp1 += 16
		loop
		object.xpos = temp2
		object.ypos = temp3
	next
end event


event ObjectDraw
	DrawSprite(0)
	temp0 = 0
	temp1 = 16
	while temp0 < object.value1
		Cos(temp2, object.angle)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		Sin(temp3, object.angle)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		DrawSpriteXY(1, temp2, temp3)
		temp0++
		temp1 += 16
	loop
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	foreach (TypeName[R Spike Chain], arrayPos0, ALL_ENTITIES)
		switch object[arrayPos0].propertyValue
		case 0
			object[arrayPos0].value0 = 5
			object[arrayPos0].value1 = 4
			break
		case 1
			object[arrayPos0].value0 = 2
			object[arrayPos0].value1 = 4
			break
		case 2
			object[arrayPos0].value0 = 2
			object[arrayPos0].value1 = 2
			break
		case 3
			object[arrayPos0].value0 = -2
			object[arrayPos0].value1 = 2
			break
		end switch
	next
	SpriteFrame(-8, -8, 16, 16, 88, 138)
	SpriteFrame(-8, -8, 16, 16, 88, 138)
	temp0 = 0
	RSpikeChain_value34 = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[R Spike Chain], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(RSpikeChain_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(RSpikeChain_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
